package com.ldy.job.utils;

import com.ldy.job.ReportUtilsApplication;
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.fields.DocPicture;

import static com.ldy.job.listener.NoModelDataListener.getLastMonth;

public class ReplacePicture {

    public static void doJob() {
        //加载Word文档
        Document doc = new Document();
        doc.loadFromFile(ReportUtilsApplication.DOC_PATH + "\\生成\\“单一窗口”推广应用月报(" + getLastMonth() + "月).doc");

        for (int i = 0; i < doc.getSections().getCount(); i++) {
            //获取文档中的指定段落
            Section section = doc.getSections().get(i);
            //批量替换图片
            for (int j = 0; j < section.getParagraphs().getCount(); j++) {
                Object obj = section.getParagraphs().get(j).getChildObjects().getFirstItem();
                if (obj instanceof DocPicture) {
                    DocPicture pic = (DocPicture) obj;
                    //创建 DocPicture 类的对象
                    DocPicture picture = new DocPicture(doc);
                    //从磁盘加载图片
                    pic.loadImage(ReportUtilsApplication.DOC_PATH + "\\生成\\ToImg.png");
                    pic.setWidth(415f);
                    pic.setHeight(263f);
                }
            }
        }

        //保存结果文档
        doc.saveToFile(ReportUtilsApplication.DOC_PATH + "\\生成\\“单一窗口”推广应用月报(" + getLastMonth() + "月).doc", FileFormat.Docx_2013);
        doc.dispose();
    }
}